<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 内容管理
 */
namespace app\content\controller;

use  Mscore\Core\Controller;
use  app\content\model\Article as ArticleModel;
use  app\content\model\Common as CommonModel;
use  Mscore\Core\Lib\ArrayMethod;
use  Mscore\Core\Lib\Upload;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lang;

class ArticleController extends Controller{

    private $model;
    private $login;
    private $error=['code'=>0,'msg'=>'fail'];
    private $success=['code'=>200,'msg'=>'success']; 
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->model = new ArticleModel;
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
        $this->language = isset($_COOKIE['language'])?$_COOKIE['language']:'default';
        Lang::init($this->language);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }   

    /**
     * 内容类别设置列表
     * @param array $articlecatelist 
     * @param array $where 
     */ 
    public function catelist(){  
            $this->display('cate_list.html');
    }

    /**
     * 内容类别设置列表
     * @param array $catelistdata 
     * @param array $where 
     */ 
    public function catelistdata(){  
        $where = '';
        $datalist = $this->model->catelist($where);
        $data = [
            "code" => 200,
            "data" => $datalist,
            "count"=> 0,
            "msg" => 'ok'
        ];
        $this->echojson($data);
    }

    /**
     * 内容類別添加
     * @param array $cateadd 
     * @param array $where 
     */ 
    public function cateadd(){
        $params = post();
        if($params){
            //post请求则保存
            $datainfo = $this->model->catesave($params);
            switch ($datainfo) {
                case true:
                    $this->echoJson($this->success); 
                    break;
                
                default:
                    $this->echoJson($this->error); 
                    break;
            }
        }else{
            if(get('id')){
                $datainfo =$this->model->cateinfo(get('id'));
            }
            
            $choice = ArrayMethod::recurse((new CommonModel)->getcateclass(' pid=0 ','id,title,acode,pid'));
            //定义变量datainfo输出
            $datainfo['id']=isset($datainfo['id'])?$datainfo['id']:'';
            $datainfo['title']=isset($datainfo['title'])?$datainfo['title']:'';
            $datainfo['titles']=isset($datainfo['titles'])?$datainfo['titles']:array();
            $datainfo['introduction']=isset($datainfo['introduction'])?$datainfo['introduction']:array();
            $datainfo['pid']=isset($datainfo['pid'])?$datainfo['pid']:0;
            $datainfo['acode']=isset($datainfo['acode'])?$datainfo['acode']:'';
            $datainfo['pagecode']=isset($datainfo['pagecode'])?$datainfo['pagecode']:'';
            $datainfo['orderid']=isset($datainfo['orderid'])?$datainfo['orderid']:1;
            $datainfo['status']=isset($datainfo['status'])?$datainfo['status']:1;
            $datainfo['imgurl']=isset($datainfo['imgurl'])?$datainfo['imgurl']:'';
            $datainfo['mimgurl']=isset($datainfo['mimgurl'])?$datainfo['mimgurl']:'';
            $datainfo['url']=isset($datainfo['url'])?$datainfo['url']:'/home/web/arcate/';
            $datainfo['is_index']=isset($datainfo['is_index'])?$datainfo['is_index']:1;
            $datainfo['is_target']=isset($datainfo['is_target'])?$datainfo['is_target']:0;

            $langs=(new CommonModel)->getLang();
            $this->assign('langs',$langs);       
            $this->assign('datainfo', $datainfo);
            $this->assign('choice', $choice);
            $this->display('cate_add.html');
        }
    }

    /**
     * 内容類別删除
     * @param array $articlecatedel 
     * @param array $where 
     */
    public function catedel(){
        $datainfo=$this->model->catedel(post('id',false));
        if($datainfo){
        	$this->echoJson($this->success); 
        }
        $this->echoJson($this->error); 
    }

    /**
     * 内容列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list(){  
    //文章类
        $post = post();
        switch (true) {
            case $post:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = ' dels<1 ';
                if(post('title')){
                    $where .= ' and title like "%'.post('title').'%"';
                }
                if(post('author')){
                    $where .= ' and author like "%'.post('author').'%"';
                }
                $datalist = $this->model->list($where,$page,$limit);
                $data = [
                    "code" => 200,
                    "data" => $datalist['datalist'],
                    "count"=> $datalist['total'],
                    "msg" => 'ok'
                ];
                $this->echojson($data);
                break;
            default:
                $this->display('list.html');
                break;
        }
    }

    /**
     * 内容添加
     * @param array $add 
     * @param array $where 
     */ 
    public function add(){
        $params = post();
        switch (true) {
            case $params:
                $datainfo = $this->model->save($params);
                switch ($datainfo) {
                    case true:
                        $this->echoJson($this->success); 
                        break;
                
                    default:
                        $this->echoJson($this->error); 
                        break;
                }
                break;
            default:
                if(get('id')){
                    $datainfo =$this->model->info(get('id'));
                }
                $class = ArrayMethod::recurse((new CommonModel)->getcateclass('','id,title,pid'));
                //定义变量datainfo输出
                $datainfo['id']=isset($datainfo['id'])?$datainfo['id']:'';
                $datainfo['title']=isset($datainfo['title'])?$datainfo['title']:'';
                $datainfo['titles']=isset($datainfo['titles'])?$datainfo['titles']:array();
                $datainfo['cid']=isset($datainfo['cid'])?$datainfo['cid']:0;
                $datainfo['keywords']=isset($datainfo['keywords'])?$datainfo['keywords']:'';
                $datainfo['description']=isset($datainfo['description'])?$datainfo['description']:'';
                $datainfo['createtime']=isset($datainfo['createtime'])?$datainfo['createtime']:date('Y-m-d H:m',time());
                $datainfo['imgurl']=isset($datainfo['imgurl'])?$datainfo['imgurl']:'';
                $datainfo['mimgurl']=isset($datainfo['mimgurl'])?$datainfo['mimgurl']:'';
                $datainfo['is_open']=isset($datainfo['is_open'])?$datainfo['is_open']:0;
                $datainfo['introduction']=isset($datainfo['introduction'])?$datainfo['introduction']:array();
                $datainfo['content']=isset($datainfo['content'])?$datainfo['content']:array();
                $datainfo['open_url']=isset($datainfo['open_url'])?$datainfo['open_url']:'';
                $datainfo['author']=isset($datainfo['author'])?$datainfo['author']:'admin';
                $datainfo['is_index']=isset($datainfo['is_index'])?$datainfo['is_index']:1;
                $datainfo['status']=isset($datainfo['status'])?$datainfo['status']:0;

                $langs=(new CommonModel)->getLang();
                $this->assign('langs',$langs);
                $this->assign('datainfo',$datainfo);
                $this->assign('class',$class);
                $this->display('add.html');
                break;
        }
    }

    /**
     * 类容添加
     * @param array $detail 
     * @param array $where 
     */ 
    public function detail(){
        $this->display('detail.html');
    }

    /**
     * 内容删除
     * @param array $del 
     * @param array $where 
     */
    public function del(){
        $img=$this->model->info(post('id'),'imgurl');
        if(isset($img['imgurl'])&&file_exists(config('file.path') . $img['imgurl'])){
            unlink (config('file.path') . $img['imgurl']); 
        }
        // if(isset($img['mimgurl'])&&file_exists(config('file.path') . $img['mimgurl'])){
        //     unlink (config('file.path') . $img['mimgurl']); 
        // }
        $datainfo=$this->model->del(post('id',false));
       switch ($datainfo) {
            case true:
                $this->echoJson($this->success); 
                break;
                
                default:
                $this->echoJson($this->error); 
                break;
        }
    }   
}
